using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
using Moq;

using Zhaoxi.Zhihu.Infrastructure;
using Zhaoxi.Zhihu.Infrastructure.Interceptors;
using Zhaoxi.Zhihu.Infrastructure.Repositories;
using Zhaoxi.Zhihu.SharedKerel.Repositoy;
using Zhaoxi.Zhihu.UseCases;
using Zhaoxi.Zhihu.UseCases.Common.Interface;
using Zhaoxi.Zhihu.UseCases.Common.Queries.Interfaces;

namespace TestProject1;

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        var mockUser = new Mock<IUser>();
        mockUser.Setup(user=>user.Id).Returns(1);
        services.AddSingleton(mockUser.Object);

        ConfigureEfCore(services);
        services.AddScoped<DbInitializer>();

        services.AddUserCaseServices();
    }

    private static void ConfigureEfCore(IServiceCollection services)
    {
        services.AddScoped<ISaveChangesInterceptor,AuditEntityInterceptor>();

        services.AddDbContext<AppDbContext>((sp, options) =>
        {
            options.AddInterceptors(sp.GetService<ISaveChangesInterceptor>());

            options.UseSqlite("Data Source=:memory:");
        });
        
        services.AddScoped(typeof(IReadRepository<>),typeof(EfReadRepository<>));
        services.AddScoped(typeof(IRepository<>),typeof(EfRepository<>));

        services.AddScoped<IDaraQueryService, DataQueryService>();
    }
}